package org.cheniy.domain.strategy.service.rule.tree.factory.engine.impl;

import lombok.extern.slf4j.Slf4j;
import org.cheniy.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.cheniy.domain.strategy.model.valobj.RuleTreeNodeLineVO;
import org.cheniy.domain.strategy.model.valobj.RuleTreeNodeVO;
import org.cheniy.domain.strategy.model.valobj.RuleTreeVO;
import org.cheniy.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.cheniy.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.cheniy.domain.strategy.service.rule.tree.factory.engine.IDecisionTreeEngine;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author cheniy
 * @description 工厂引擎-进行执行规则树的路径判断
 * @create 2025/3/8 15:40
 */
@Slf4j
public class DecisionTreeEngine implements IDecisionTreeEngine {


    // 由工厂外部提供下列属性
    private final Map<String, ILogicTreeNode> logicTreeNodeGroup;

    private final RuleTreeVO ruleTreeVO;

    public DecisionTreeEngine(Map<String, ILogicTreeNode> logicTreeNodeGroup, RuleTreeVO ruleTreeVO) {
        this.logicTreeNodeGroup = logicTreeNodeGroup;
        this.ruleTreeVO = ruleTreeVO;
    }

    @Override
    public DefaultTreeFactory.StrategyAwardVO process(String userId, Long strategyId, Integer awardId, Date endDateTime) {
        DefaultTreeFactory.StrategyAwardVO strategyAwardData=null;

        // 1. 获取到头节点名称
        String nextNode = ruleTreeVO.getTreeRootRuleNode();

        // 2. 获取到所有的节点，并获取都爱头节点节点实体
        Map<String, RuleTreeNodeVO> treeNodeMap = ruleTreeVO.getTreeNodeMap();
        RuleTreeNodeVO ruleTreeNode = treeNodeMap.get(nextNode);
        // 3.

        while (null!=nextNode){

            // 4. 获取到节点实体的对应过滤器，进行执行
            ILogicTreeNode logicTreeNode = logicTreeNodeGroup.get(nextNode);
            String ruleValue = ruleTreeNode.getRuleValue();
            DefaultTreeFactory.TreeActionEntity treeActionEntity = logicTreeNode.logic(userId, strategyId, awardId, ruleValue, endDateTime);

            // 5. 获取到执行的结果-放行为null，拦截（幸运奖）则返回奖品id和幸运奖比值
            strategyAwardData=treeActionEntity.getStrategyAwardVO();
            RuleLogicCheckTypeVO ruleLogicCheckTypeVO = treeActionEntity.getRuleLogicCheckType();
            log.info("决策树引擎【{}】treeId:{} node:{} code:{}", ruleTreeVO.getTreeName(), ruleTreeVO.getTreeId(), nextNode, ruleLogicCheckTypeVO.getCode());
            // 6. 获取下个节点
            nextNode = nextNode(ruleLogicCheckTypeVO.getCode(),ruleTreeNode.getTreeNodeLineVOList());
            ruleTreeNode = treeNodeMap.get(nextNode);

        }

        return strategyAwardData;
    }

    // 判断
    private String nextNode(String matterValue, List<RuleTreeNodeLineVO> ruleTreeNodeLineVOS){

        // 表示该节点没有叶子节点了
        if (null==ruleTreeNodeLineVOS||ruleTreeNodeLineVOS.isEmpty()) return null;
        for (RuleTreeNodeLineVO ruleTreeNodeLineVO:ruleTreeNodeLineVOS){
            if (decisionLogic(matterValue,ruleTreeNodeLineVO)){
                return ruleTreeNodeLineVO.getRuleNodeTo();
            }
        }

        return null;
    }

    private boolean decisionLogic(String matterValue, RuleTreeNodeLineVO nodeLine){
        switch (nodeLine.getRuleLimitType()){
            case EQUAL:
                return matterValue.equals(nodeLine.getRuleLimitValue().getCode());

            default:
                return false;
        }
    }

}
